package org.example.platform.beans.factory;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 核心类实现
 */
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {
    Map<String,BeanDefinition> map = new ConcurrentHashMap<>();

    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        map.put(beanName,beanDefinition);
    }

    @Override
    protected BeanDefinition getBeanDefinition(String beanName) throws Exception {
        BeanDefinition beanDefinition = map.get(beanName);
        if(beanDefinition == null ){
            throw  new Exception("bean is defined");
        }
        return beanDefinition;
    }


    @Override
    public Object getBean(String name) throws Exception {
        Object bean = getSingleton(name);
        if(null != bean){
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name,beanDefinition,null);
    }

}
